package com.google.code.sprite.util;

import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class Util {

    public static String readContent(File file) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            while (true) {
                String tmp = br.readLine();
                if (tmp == null) break;
                sb.append(tmp).append("\n");
            }
        } catch (IOException e) {
        } finally {
            if (br !=null ) try { br.close(); } catch (Exception e) {}
        }
        return sb.toString();
    }

    public static List<String>  fitToPrintWidth(List<String> fields, int width) {
        StringBuilder sb = new StringBuilder();
        List<String> result = new ArrayList<String>();
        for (String value : fields ) {
            if (sb.length() + value.length() > width ) {
                result.add(sb.toString());
                sb = new StringBuilder();
            } 
            sb.append(value).append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        result.add(sb.toString());
        return result;
    }

    public static String getQuestionMark(int size) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append("?");
        }
        return sb.toString();
    }


    public static String normalize(String dbField) {
    	dbField = dbField.toLowerCase();
        String[] tokens = dbField.split("_");
        StringBuilder result = new StringBuilder();
        int index = 0; 
        for (String token : tokens) {
            if (token.trim().equals("")) {
                continue;
            }
            if ( index++ == 0 ) {
                result.append(token);
            }else {
                result.append(capatilize(token));
            }
        }
        return result.toString();
    }

    public static String capatilize(String old) {
        if ( old == null ) return null;
        if (old.length() ==0 ) return old;
        if (old.length() == 1) return old.toUpperCase();
        return old.substring(0,1).toUpperCase() + old.substring(1);
    }

    public static String decapatilize(String old) {
        if ( old == null ) return null;
        if (old.length() ==0 ) return old;
        if (old.length() == 1) return old.toLowerCase();
        return old.substring(0,1).toLowerCase() + old.substring(1);
    }

    public static String getJavaType(int dataType) {
        if (dataType == Types.INTEGER) {
            return "int";
        }
        else if (dataType == Types.TIMESTAMP || dataType == Types.DATE
                || dataType == Types.TIME) {
            return "Date";
                }
        else {
            return "String";
        }
    }

    public static String getResultSetMethod(int dataType) {
        if (dataType == Types.INTEGER) {
            return "getInt";
        }
        else if (dataType == Types.TIMESTAMP || dataType == Types.DATE
                || dataType == Types.TIME) {
            return "getTimestamp";
                }
        else {
            return "getString";
        }
    }
}
